//
//  HMHomeViewCell.swift
//  Weibo
//
//  Created by ZhuDong on 16/5/24.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class HMHomeViewCell: UITableViewCell {
    var toolBarTopCons: Constraint?
    var statusesViewModel: HMStatusViewModel?{
        didSet{
            toolBarView.statusViewModel = statusesViewModel
           headerView.statusViewModel = statusesViewModel
//            retweetView.statusViewModel = statusesViewModel
            toolBarTopCons?.uninstall()
            if statusesViewModel?.status?.retweeted_status == nil {
                retweetView.statusViewModel = nil
                retweetView.hidden = true
                toolBarView.snp_makeConstraints(closure: { (make) in
                    toolBarTopCons = (make.top.equalTo(headerView.snp_bottom)).constraint
                })
            }else{
                retweetView.statusViewModel = statusesViewModel
                retweetView.hidden = false
                toolBarView.snp_updateConstraints(closure: { (make) in
                    toolBarTopCons = (make.top.equalTo(retweetView.snp_bottom)).constraint
                })
            }
        }
    }
    private lazy var headerView: HMHeaderView = HMHeaderView()
    private lazy var retweetView: HMRetweetView = HMRetweetView()
    private lazy var toolBarView: HMToolbarView = HMToolbarView()
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        selectionStyle = .None
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupUI() {
        contentView.addSubview(headerView)
        contentView.addSubview(toolBarView)
        contentView.addSubview(retweetView)
        headerView.snp_makeConstraints { (make) in
            make.top.left.right.equalTo(self.contentView)

        }
        retweetView.snp_makeConstraints { (make) in
            make.top.equalTo(headerView.snp_bottom)
            make.left.right.equalTo(contentView)
        }
        toolBarView.snp_makeConstraints { (make) in
            make.left.right.equalTo(contentView)
            make.height.equalTo(36)
            toolBarTopCons = (make.top.equalTo(retweetView.snp_bottom)).constraint
        }
        contentView.snp_makeConstraints { (make) in
            make.bottom.equalTo(toolBarView.snp_bottom)
            make.top.left.right.equalTo(self)
        }
    }
}
